package cn.walls1717.wallsmall.order.listener;

import cn.walls1717.wallsmall.order.entity.OrderEntity;
import cn.walls1717.wallsmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 订单关闭监听
 *
 * @author walls1717
 * @since 2022/11/23 11:39
 **/
@Service
@RabbitListener(queues = "order.release.order.queue")
@Slf4j
public class OrderCloseListener {
    @Resource
    private OrderService orderService;

    /** 关闭过期订单 */
    @RabbitHandler
    public void listener(OrderEntity order, Channel channel, Message message) throws IOException {
        log.info("准备关闭过期订单 -> {}", order.getOrderSn());
        try {
            orderService.closeDelayOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
